package com.yckj.activechicken.business.common.controller;

import com.yckj.activechicken.business.common.model.CustomSequence;
import com.yckj.activechicken.business.common.service.ISequenceService;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.common.utils.poi.ExcelUtil;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.common.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【序列管理请求控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/7 21:09
 */
@RestController
@RequestMapping("/system/sequence")
public class SequenceController extends BaseController{

    @Autowired
    private ISequenceService sequenceService;

    /**
     * 新增序列
     * @param customSequence
     * @return
     */
    @RequestMapping("/addSequence")
    public AjaxResult addSequence(@RequestBody CustomSequence customSequence){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();

        customSequence.setCurrentValueStr(customSequence.getCurrentValue()+"");
        customSequence.setCreateBy(username);
        customSequence.setUpdateBy(username);
        customSequence.setCreateTime(newDate);
        customSequence.setUpdateTime(newDate);

        Map<String,Object> outMap = sequenceService.addSequence(customSequence);
        String result = (String) outMap.get("result");
        String message = (String) outMap.get("message");

        if(StringUtils.equals(result,"true")){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(message);
        }
    }

    /**
     * 分页查询序列信息
     * @param customSequence
     * @return
     */
    @GetMapping("/listSequence")
    public TableDataInfo listSequence(CustomSequence customSequence) {
        startPage();
        List<CustomSequence> list = sequenceService.queryList(customSequence);
        return getDataTable(list);
    }


    /**
     * 修改序列信息
     * @param customSequence
     * @return
     */
    @RequestMapping("/modifySequence")
    public AjaxResult modifySequence(@RequestBody CustomSequence customSequence){
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();

        customSequence.setCurrentValueStr(customSequence.getCurrentValue()+"");
        customSequence.setUpdateBy(username);
        customSequence.setUpdateTime(newDate);

        return toAjax(sequenceService.modifySequence(customSequence));
    }

    /**
     * 导出序列信息
     * @param customSequence
     * @return
     */
    @GetMapping("/exportSequence")
    public AjaxResult exportSequence(CustomSequence customSequence) {
        List<CustomSequence> list = sequenceService.queryList(customSequence);
        ExcelUtil<CustomSequence> util = new ExcelUtil<CustomSequence>(CustomSequence.class);
        return util.exportExcel(list, "序列数据");
    }
}
